package com.ccp.dev.workflow.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.workflow.model.bpm.BpmRunLog;
import com.ccp.dev.workflow.service.bpm.BpmRunLogService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * ClassDescribe:流程运行日志 控制器类
 * @author :jiayueqiao
 * Date: 2019-10-16
 * Since:1
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_WORKFLOW + "/bpmRunLog")
@Action(ownermodel=SysAuditModelType.PROCESS_MANAGEMENT)
public class BpmRunLogController extends SysBaseController {
    private static final String SUCCESS = "operation.success";
    private static final String FAILURE = "operation.failure";

    @Resource
    private BpmRunLogService bpmRunLogService;

    /**
     * 流程操作日志列表
     * @param request request
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="查看流程运行日志分页列表",detail="查看流程运行日志分页列表<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData list(HttpServletRequest request)
    {
        try {
            //判断是否为超管
            boolean isAdmin = ContextUtil.isSuperAdmin();
            List<BpmRunLog> list;
            String runId= RequestUtil.getString(request, "runId");
            QueryFilter filter=new QueryFilter(request,true);
            if(StringUtil.isNotEmpty(runId)){
                filter.addFilter("runId", runId);
            }
            if(isAdmin){
                list=bpmRunLogService.getAll(filter);
            } else {
                //获取当前登录用户的租户ID
                String tenantId = ContextUtil.getCurrentUserTenantId();

                filter.addFilter("tenantId", tenantId);
                list=bpmRunLogService.getAll(filter);
            }
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess(SUCCESS, filter.getPageBean().getTotalCount(), list);
        }catch (Exception e){
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError(FAILURE,1,e.getMessage());
        }
    }

    /**
     * 流程操作日志页面跳转
     * @return String
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="查看流程操作日志页面",detail="查看流程操作日志页面")
    public String viewRunningProcessLogList(Model model) {
        SysUser user = (SysUser) ContextUtil.getCurrentUser();
        model.addAttribute("privacyLevel", user.getPrivacyLevel());
        return getAutoView();
    }


    /**
     * 我的流程操作日志
     * @param request request
     * @return ResultData
     */
    @RequestMapping("/myList")
    @Action(description="查看我的流程运行日志分页列表",detail="查看我的流程运行日志分页列表<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData myList(HttpServletRequest request) {
        String userId= ContextUtil.getCurrentUserId();
        QueryFilter filter=new QueryFilter(request,true);
        filter.addFilter("userId", userId);
        try {
            List<BpmRunLog> list=bpmRunLogService.getAll(filter);
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess(SUCCESS, filter.getPageBean().getTotalCount(), list);
        }catch (Exception e){
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError(FAILURE,1,e.getMessage());
        }

    }
    /**
     * 我的流程操作日志页面跳转
     * @return String
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/myList")
    @Action(description="查看我的流程日志页面",detail="查看我的流程日志页面")
    public String viewRunningProcessMyLogList() {
        return getAutoView();
    }

    /**
     * 删除流程操作日志
     * @param request request
     * @return ResultData
     */
    @RequestMapping(value = UrlConstants.MODULE_FUNCTION_DELETE, method = RequestMethod.GET)
    @Action(description="删除流程运行日志",
            detail="删除流程运行日志，日志id包含${ids}，操作<#if fl>成功<#else>失败</#if>"
    )
    @ResponseBody
    public ResultData del(HttpServletRequest request) {
        try {
            String ids = request.getParameter("id");
            SysAuditThreadLocalHolder.putParamerter("ids", ids);
            String[] lAryId = RequestUtil.getStringAryByStr(request, "id");
            bpmRunLogService.delByIds(lAryId);
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess("delete.success");
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("delete.failure",1,e.getMessage());
        }
    }
    /**
     * 取得流程运行日志明细
     * @param request request
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @Action(description="查询流程操作日志明细数据",detail="查询流程操作日志明细数据<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData get(HttpServletRequest request)
    {
        String id=RequestUtil.getString(request,"id");
        try {
            BpmRunLog bpmRunLogGet = bpmRunLogService.getById(id);
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess(SUCCESS,0,bpmRunLogGet);
        }catch (Exception e){
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError(FAILURE,1,e.getMessage());
        }
    }

    /**
     * 流程运行日志明细页面跳转
     * @param request request
     * @param model 视图模型对象
     * @return String
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX  + UrlConstants.MODULE_FUNCTION_GET)
    @Action(description="查看流程操作日志明细页面",detail="查看流程操作日志明细页面")
    public String viewGet(HttpServletRequest request, Model model) {
        String id=RequestUtil.getString(request,"id");
        model.addAttribute("id",id);
        return getAutoView();
    }

}
